package dao.impl;

import dao.UsersDao;
import entity.Users;
import entity.vo.MessageData;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import utils.DruidUtils;

import java.sql.Connection;
import java.util.List;

public class UsersDaoImpl implements UsersDao {


    @Override
    public Users selectOneUser(String phoneNumber, String password) {
        Connection conn = null;
        try {
            //从连接池获取连接
            conn = DruidUtils.getConnection();
            String s = "select * from users where phone_number=? and password=?";

            QueryRunner queryRunner = new QueryRunner();
            BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());
            List<Users> query = queryRunner.query(conn, s, new BeanListHandler<>(Users.class, basicRowProcessor), phoneNumber, password);
            DruidUtils.close(null, conn);
            if (!query.isEmpty()) return query.get(0);
            else return null;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //归还连接(没有Statement)
            DruidUtils.close(null, conn);
        }
        return null;
    }

    @Override
    public List<Users> getAllUsers() {
        Connection conn = null;
        try {
            //从连接池获取连接
            conn = DruidUtils.getConnection();
            String s = "select * from users where role!='admin'";

            QueryRunner queryRunner = new QueryRunner();
            BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());
            List<Users> query = queryRunner.query(conn, s, new BeanListHandler<>(Users.class, basicRowProcessor));
            DruidUtils.close(null, conn);
            if (!query.isEmpty()) return query;
            else return null;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //归还连接(没有Statement)
            DruidUtils.close(null, conn);
        }
        return null;
    }
    @Override
    public boolean insertUser(Users users) {
        int update = 0;
        Connection conn = null;
        try {
            //从连接池获取连接
            conn = DruidUtils.getConnection();

            String s = "select count(*) from users where phone_number=?";
            QueryRunner queryRunner = new QueryRunner();

            Long query = queryRunner.query(conn, s, new ScalarHandler<Long>(), users.getPhoneNumber());
            if (query != 0L) {
                return false;//该账号已经呗注册
            }
            s = "insert into users (name, password,  phone_number, role) values (?,?,?,?)";
            update += queryRunner.update(conn, s, users.getName(),
                    users.getPassword(), users.getPhoneNumber(), users.getRole().toString()); //修改加一
            if (users.getRole().toString().equals("admin")) {
                s = "select id from users where phone_number=?";
                //获取该用户的Id
                Integer query1 = queryRunner.query(conn, s, new ScalarHandler<Integer>(), users.getPhoneNumber());
                s = "insert into admins ( user_id) VALUES (? )";
                update += queryRunner.update(conn, s, query1);  //修改加一
            }
            if (users.getRole().toString().equals("doctor")) {
                s = "select id from users where phone_number=?";
                //获取该用户的Id
                Integer query1 = queryRunner.query(conn, s, new ScalarHandler<Integer>(), users.getPhoneNumber());
                s = "insert into doctors ( user_id) VALUES (? )";
                update += queryRunner.update(conn, s, query1);  //修改加一
            }
            DruidUtils.close(null, conn);
            if (update > 1) return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //归还连接(没有Statement)
            DruidUtils.close(null, conn);
        }
        return false;
    }

    @Override
    public Users queryById(Integer id) {
        return null;
    }

    @Override
    public boolean updateById(Users users) {
        Connection conn = null;
        try {
            //从连接池获取连接
            conn = DruidUtils.getConnection();
            String s = "update users set name=?, phone_number=?,email=? where id=?";
            QueryRunner queryRunner = new QueryRunner();
            int update = queryRunner.update(conn, s, users.getName(), users.getPhoneNumber(), users.getEmail(), users.getId());
            DruidUtils.close(null, conn);
            if (update > 0) return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //归还连接(没有Statement)
            DruidUtils.close(null, conn);
        }
        return false;
    }


    @Override
    public boolean changePassword(String oldPassword, String newPassword, Integer id) {
        Connection conn = null;
        try {
            //从连接池获取连接
            conn = DruidUtils.getConnection();
            String s = "select * from users where id=? and password=?";

            QueryRunner queryRunner = new QueryRunner();
            BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());
            List<Users> query = queryRunner.query(conn, s, new BeanListHandler<>(Users.class, basicRowProcessor), id, oldPassword);
            if (query.isEmpty()) return false;
            s = "update users set password=?where id=?";
            int update = queryRunner.update(conn, s, newPassword, id);
            DruidUtils.close(null, conn);
            if (update > 0) return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //归还连接(没有Statement)
            DruidUtils.close(null, conn);
        }
        return false;
    }

    @Override
    public int deleteById(Integer id) {
        Connection conn = null;
        try {
            //从连接池获取连接
            conn = DruidUtils.getConnection();
            String s = "delete  from users where id=?";
            QueryRunner queryRunner = new QueryRunner();
            int update = queryRunner.update(conn, s, id);
            System.out.println("----------------------------修改行数:" + update);
            DruidUtils.close(null, conn);
            return update;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //归还连接(没有Statement)
            DruidUtils.close(null, conn);
        }
        return 0;
    }
}
